<?php
require_once("db_functions.php");
require_once("adrese.class.php");
class komitentObject extends page{
	var $id;
	var $name;
	var $name1;
	var $struktura;
	var $tip;
	var $pib;
	var $matbr;
	var $adresa;
	var $vlasnik_name;
	var $vlasnik_jmbg;
	var $direktor;
	var $direktor_tel;
	var $predstavnik;
	var $predstavnik_tel;
	var $napomena;
	var $view_status;
	var $toll;
	var $dat_osn;
	var $poreklo;
	var $zasluga;
	
	var $profil_srp;
	var $profil_eng;
	var $np = array();
	var $zr = array();
	var $tel = array();
	var $fax = array();
	var $email = array();
	var $web = array();
	var $brand = array();
	var $rv = array("rd"=>"","sub"=>"","ned"=>"");			// radni_dan,subota,nedelja
	var $delatnosti = array();
	
	var $db;
	var $address_mask;
	
	/**
	 * Komitent object
	 *
	 * @param int $id
	 * @return komitent
	 */
	function komitentObject($id=""){
		$this->address_mask = new adrese();
		$this->db = new db_functions();
		if($id){
			$this->id = $id;
			$this->fillData();
		}
	}
	/**
	 * Fill komitentObject with
	 * data passed from DB.
	 */
	function fillData(){
		
	 	 /*******************************\
		|								  |
		|		get all db resources	  |
		|								  |
		 \*******************************/
		
	//komitenti
		list($tbl_komitenti)= $this->db->select2("SELECT * FROM komitenti WHERE komitent_id=".$this->id);
	//brands
		$tbl_brands = $this->db->select2("SELECT kb.del_id, kb.brand_id, brands.brand_name FROM brands, komitenti_brands kb WHERE kb.brand_id=brands.brand_id AND kb.kom_id=".$this->id." ORDER BY brands.brand_name");
	//profili
		$tbl_profili = $this->db->select2("SELECT * FROM profili WHERE profil_kom_id=".$this->id);
	//nacini placanja
		$tbl_nacplac = $this->db->select2("SELECT np.np_id, np.np_name, np.np_sifra FROM komitenti_np knp, nacini_placanja np WHERE knp.np_id=np.np_id AND knp.kom_id=".$this->id);
	//delatnosti
		$q = "SELECT komitenti_delatnosti.*, delatnosti.delatnost_ime "
			."FROM komitenti_delatnosti, delatnosti "
			."WHERE komitenti_delatnosti.komdel_del_id=delatnosti.delatnost_id "
			."AND komdel_kom_id=".$this->id." "
			."ORDER BY delatnosti.delatnost_ime";
			;
		$tbl_delatnosti = $this->db->select2($q);
	//mails
		$tbl_emails = $this->db->select2("SELECT DISTINCT * FROM komitenti_emails WHERE kom_id=".$this->id." ORDER BY email");
	//kampanje
		$tbl_kampanje = $this->db->select2("SELECT * FROM komitenti_kampanje WHERE komitent_id=".$this->id);
	//kartice
		$tbl_kartice = $this->db->select2("SELECT kk.komdel_id, kd.komdel_del_id, kd.komdel_adresa_id, kk.card_id, kartice.card_name FROM komitenti_kartice kk, kartice, komitenti_delatnosti kd WHERE kk.komdel_id=kd.komdel_id AND kk.card_id=kartice.card_id AND kd.komdel_kom_id=".$this->id);
	//keywords
		$tbl_keywords = $this->db->select2("SELECT DISTINCT kw.del_id, kr.kword_lang, kr.kword FROM komitenti_kwords kw, kljucne_reci kr WHERE kw.kword_id=kr.kword_id AND kw.kom_id=".$this->id);
	//zasluga
		$tbl_radnici = $this->db->select2("SELECT komrad_radnik_id, radnik_ime, radnik_prezime FROM komitenti_radnici, radnici WHERE komitenti_radnici.komrad_radnik_id=radnici.radnik_id AND komitenti_radnici.komrad_komitent_id=".$this->id." ORDER BY komrad_datum DESC");
	//radno vreme
		$q = "SELECT kr.* FROM komitenti_rvreme kr WHERE kr.kom_id=".$this->id;
		$tbl_rvreme = $this->db->select2($q);
	//struktura
		$strukt_name = $this->db->getOne("SELECT kom_strukt_name FROM komitenti_strukture, komitenti WHERE komitent_str_id=kom_strukt_id AND komitent_id=".$this->id);
	//telefoni i faxovi
		$tbl_telefoni = $this->db->select2("SELECT * FROM komitenti_telefoni WHERE kom_id=".$this->id);
	//tip
		$tip_name = $this->db->getOne("SELECT kom_tip_name FROM komitenti_tipovi, komitenti WHERE komitenti.komitent_tip_id=komitenti_tipovi.kom_tip_id AND komitenti.komitent_id=".$this->id);
	//web adrese
		$tbl_webs = $this->db->select2("SELECT DISTINCT web, del_id FROM komitenti_webs WHERE kom_id=".$this->id." ORDER BY web");
	//ziro racuni
		$tbl_zr = $this->db->select2("SELECT ziro_id, ziro_value, banka_id, banka_name FROM komitenti_ziro_racuni, banke WHERE banka_id=ziro_banka_id AND ziro_komitent_id=".$this->id." ORDER BY banka_name");
	
		
	// komitenti
		$this->name			= $tbl_komitenti["komitent_name"];
		$this->name1		= $tbl_komitenti["komitent_name1"];
		$this->struktura	= $tbl_komitenti["komitent_str_id"];
		$this->tip			= $tbl_komitenti["komitent_tip_id"];
		$this->pib			= $tbl_komitenti["komitent_pib"];
		$this->matbr		= $tbl_komitenti["komitent_matbr"];
		$this->vlasnik_name	= $tbl_komitenti["komitent_vlasnik_name"];
		$this->vlasnik_jmbg	= $tbl_komitenti["komitent_vlasnik_jmbg"];
		$this->direktor		= $tbl_komitenti["komitent_direktor"];
		$this->direktor_tel	= $tbl_komitenti["komitent_direktor_tel"];
		$this->predstavnik	= $tbl_komitenti["komitent_predst"];
		$this->predstavnik_tel=$tbl_komitenti["komitent_predst_tel"];
		$this->napomena		= $tbl_komitenti["komitent_napomena"];
		$this->view_status	= $tbl_komitenti["komitent_view_status"];
		$this->toll			= $tbl_komitenti["komitent_toll"];
		$this->dat_osn		= $tbl_komitenti["komitent_dat_osn"];
		$this->poreklo		= $tbl_komitenti["komitent_poreklo"];
		
	//adresa
		$this->adresa = $tbl_komitenti["komitent_adresa"];
		//$adr = $this->address_mask->getAll($tbl_komitenti["komitent_adresa"]);
		//debug($adr);
		
	//profili
		if($profs = $this->getItem(0,$tbl_profili,"profil_del_id",array("profil_lang","profil_text"))){
			list($this->profil_srp) = $this->getItem("srp",$profs,"profil_lang","profil_text");
			list($this->profil_eng) = $this->getItem("eng",$profs,"profil_lang","profil_text");
		}
	//nacini placanja
		if($tbl_nacplac){
			foreach ($tbl_nacplac as $np){
				$tmp["name"] = $np["np_name"];
				$tmp["sifra"]= $np["np_sifra"];
				$this->np[$np["np_id"]]=$tmp;
			}
			$tmp=null;
		}
	// ziro racuni
		if($tbl_zr){
			foreach ($tbl_zr as $z){
				$tmp["ziro"] = $z["ziro_value"];
				$tmp["banka_id"] = $z["banka_id"];
				$tmp["banka_name"] = $z["banka_name"];
				$this->zr[] = $tmp;
			}
			$tmp = null;
		}
	// telefon i fax
		if($tels = $this->getItem(0,$tbl_telefoni,"del_id",array("telefon","fax"))){
			$this->tel = $this->getItem(0,$tels,"fax","telefon");
			$this->fax = $this->getItem(1,$tels,"fax","telefon");
		}
	// email
		if($mails = $this->getItem(0,$tbl_emails,"del_id","email")){
			$this->email = $mails;
		}
	// web
		if($webs = $this->getItem(0,$tbl_webs,"del_id","web")){
			$this->web = $webs;
		}
	// brand
		if($brands = $this->getItem(0,$tbl_brands,"del_id",array("brand_id","brand_name"))){
			foreach ($brands as $brand){
				$this->brand[$brand["brand_id"]] = $brand["brand_name"];
			}
		}
	// radno vreme
		if(list($rvs) = $this->getItem(0,$tbl_rvreme,"komdel_id",array("rad_dan","subota","nedelja"))){
			$this->rv["rd"] = $rvs["rad_dan"];
			$this->rv["sub"]= $rvs["subota"];
			$this->rv["ned"]= $rvs["nedelja"];
		}
	/***************************************
	 				delatnosti
	****************************************/
		if($dels = $tbl_delatnosti){
			foreach ($dels as $del){
				
				if(!array_key_exists($del["komdel_del_id"],$this->delatnosti)){
					$d = new komitent_delatnost($del["komdel_del_id"]);
					$d->name = $del["delatnost_ime"];
					$d->glavna = $del["glavna"];
				//profili
					if($profili = $this->getItem($d->id,$tbl_profili,"profil_del_id",array("profil_id","profil_lang","profil_text"))){
						$d->profil_srp = $this->getItem("srp",$profili,"profil_lang","profil_text");
						$d->profil_srp = $d->profil_srp[0];
						$d->profil_eng = $this->getItem("eng",$profili,"profil_lang","profil_text");
						$d->profil_eng = $d->profil_eng[0];
					}
				//emails
					if($emails = $this->getItem($d->id,$tbl_emails,"del_id","email")){
						$d->email = $emails;
					}
				//webs
					if($webs = $this->getItem($d->id,$tbl_webs,"del_id","web")){
						$d->web = $webs;
					}
				//brands
					if($brands = $this->getItem($d->id,$tbl_brands,"del_id",array("brand_id","brand_name"))){
						foreach ($brands as $brand){
							$d->brand[$brand["brand_id"]] = $brand["brand_name"];
						}
					}
				//kwords
					if($kwords = $this->getItem($d->id,$tbl_keywords,"del_id",array("kword_lang","kword"))){
						$d->kword_srb = $this->getItem("srb",$kwords,"kword_lang","kword");
						$d->kword_eng = $this->getItem("eng",$kwords,"kword_lang","kword");
					}
					$this->delatnosti[$del["komdel_del_id"]] = $d;
				}
				
				//$theDel = $this->delatnosti[$del["komdel_del_id"]];
				
				
				$dd = new komitent_delatnost_data();
				
				$dd->komdel = $del["komdel_id"];
			//adresa
				$dd->adresa 	= $del["komdel_adresa_id"];
			//tel i fax
				if($tbl_tel_adr = $this->getItem($dd->adresa,$tbl_telefoni,"adresa",array("del_id","telefon","fax"))){
					
					if($tels = $this->getItem($theDel->id,$tbl_tel_adr,"del_id",array("telefon","fax"))){
						$dd->tel = $this->getItem(0,$tels,"fax","telefon");
						$dd->fax = $this->getItem(1,$tels,"fax","telefon");
					}
				}
			//radno vreme
				if(list($rvr) = $this->getItem($dd->komdel,$tbl_rvreme,"komdel_id",array("rad_dan","subota","nedelja"))){
					$dd->rv["rd"]  = $rvr["rad_dan"];
					$dd->rv["sub"] = $rvr["subota"];
					$dd->rv["ned"] = $rvr["nedelja"];
				}
			//kartice
				if($tbl_kartice_adr = $this->getItem($dd->adresa,$tbl_kartice,"komdel_adresa_id",array("komdel_del_id","card_id","card_name"))){
					if($kartice = $this->getItem($theDel->id,$tbl_kartice_adr,"komdel_del_id",array("card_id","card_name"))){
						$dd->card = $kartice;
					}
				}
				
			// WWW
				//echo $dd->komdel.": ".$del["dat_pos"]."<br />";
				if($del["dat_pos"]>0 || $del["banner_name"] || $del["test_adresa"]){
					$dd->www_params = 1;
				}else{
					$dd->www_params = 0;
				}
				
				$this->delatnosti[$del["komdel_del_id"]]->data[]=$dd;
				//$theDel->data[] = $dd;
			}
		}
		//print_rarray($this);
	}
	/**
	 * Fill komitentObject with data 
	 * passed trough url string
	 *
	 * @param string url input
	 */
	function fillDataURI($data){
		$id = preg_split('/id{=}(.*?){!}/',$data,-1,PREG_SPLIT_DELIM_CAPTURE);
		$id = $id[1];
		//echo '<input type="button" value="refresh" onclick="LoadRSDoc(\'komitenti.php\',\'setContent\',\'a=save&data='.urlencode($data).'\',\'\',1)" />';
		//echo '<input type="button" value="komitent" onclick="LoadRSDoc(\'komitenti.php?a=displayOne&id='.$id.'\',\'setContent\')" />';
		$k = $this->parseURIData($data);
		
		$this->id		= $k["id"];
		$this->name		= $k["naziv"];
		$this->name1	= $k["naziv_indx"];
		$this->struktura= $k["struktura"];
		$this->tip		= $k["tip"];
		$this->pib		= $k["pib"];
		$this->matbr	= $k["matbr"];
		
		// ADRESA
			$adrid	= $k["adresa_id"];
			$ulica	= $k["ulica"];
			$ulid	= $k["ulica_id"];
			$broj	= trim($k["broj"]);
			$regid	= $k["region"];
			$opsid	= $k["opstina"];
			$mesid	= $k["mesto"];
			$lopsid	= $k["lokopstina"];
			$nasid	= $k["naselje"];
			
			// provera da li je ukucano ime ulice
			// ili je odabrano
			if(!strlen($ulid)){
				$ul_id = $this->address_mask->getUlicaId($ulica);
				if(!$ul_id){
					if($this->address_mask->addUlica($ulica)){		// unos nove ulice
						$ulid = $this->address_mask->getUlicaId($ulica);
					}else{
						die("Greška pri upisu ulice!");
					}
				}else{
					$ulid = $ul_id;
				}
			}
			
			$adr = $this->address_mask->getAddressId($ulid,$broj,$regid,$opsid,$mesid,$lopsid,$nasid);
			if($adr){
				if($adr == $adrid){				// ADRESA NIJE IZMENJENA
					$this->adresa = $adrid;
					//die("adresa je ostala ista: $adr.");
				}else{							// ADRESA JE IZMENJENA - DODELI NOVU
					$this->adresa = $adr;
					//die("promenjena je adresa u vec postojecu: $adr");
				}
			}else{								// NE POSTOJI ADRESA SA ZADATIM PARAMETRIMA (DODAJ NOVU)
				
				if($nasid<1 && $lopsid<1 && $mesid<1 && $opsid<1 && $regid<1){
					$this->adresa=0;
				}else{
					if($nasid > 0){
						$lid = $nasid;
					}else{
						if($lopsid > 0){
							$lid = $lopsid;
						}else{
							$lid = $mesid;
						}
					}
					//die("Unesi novu adresu:<br />Ulica: $ulid<br />Broj: $broj<br />Region: $regid<br />Opstina: $opsid<br />Mesto: $mesid<br />Lok.opstina: $lopsid<br />Naselje: $nasid");
					// unos nove adrese
					if($this->address_mask->addAdresa($ulid,$lid,$broj)){
						$adr = $this->address_mask->getAddressId($ulid,$broj,$regid,$opsid,$mesid,$lopsid,$nasid);
						$this->adresa = $adr;
					}else{
						die("Izmene nisu sačuvane. Unknown error.");
					}
				}
			}
			
		
		// VLASNIK
			$this->vlasnik_name = $k["vlasnik"];
			$this->vlasnik_jmbg = $k["vlasnik_jmbg"];
		// DIREKTOR
			$this->direktor		= $k["direktor"];
			$this->direktor_tel	= $k["direktor_tel"];
		// PREDSTAVNIK
			$this->predstavnik		= $k["predstavnik"];
			$this->predstavnik_tel	= $k["predstavnik_tel"];
		// NAPOMENA
			$this->napomena = $k["napomena"];
		// VIEW STATUS
			$this->view_status = $k["view_status"];
		// TOLL
			$this->toll = $k["toll"];
		// ZASLUGA
			$this->zasluga = $k["zasluga"];
		// DATUM OSNIVANJA
			$this->dat_osn = $k["datum_osn"];
		// POREKLO
			$this->poreklo = $k["poreklo"];
		// PROFILI
			$this->profil_srp = $k["profilsrp"];
			$this->profil_eng = $k["profileng"];
		// NACINI PLACANJA
			$this->np = $k["nacini_placanja"];
		// ZIRO RACUNI
			$this->zr = $k["racuni"];
		// TELEFON
			$this->tel = $k["telefon"];
		// FAKS
			$this->fax = $k["faks"];
		// EMAIL
			$this->email = $k["email"];
		// WEB
			$this->web = $k["web"];
		// BRAND
			$this->brand = $k["brands"];
		// RADNO VREME
			$this->rv = $k["radnovreme"];
			
		if(count($k["delatnosti"])){
			foreach ($k["delatnosti"] as $del){
				$komdel = new komitent_delatnost($del["id"]);
				
				// NAZIV DELATNOSTI
					$komdel->name = $del["naziv"];
				// GLAVNA
					$komdel->glavna = $del["glavna"];
				// PROFILI
					$komdel->profil_srp = $del["profilsrp"];
					$komdel->profil_eng = $del["profileng"];
				// EMAIL
					$komdel->email = $del["email"];
				// WEB
					$komdel->web = $del["web"];
				// BRAND
					$komdel->brand = $del["brands"];
				// KEYWORDS
					$komdel->kword_srb = $del["kwords_srb"];
					$komdel->kword_eng = $del["kwords_eng"];
				
				// PODACI
				if($del["podaci"]){
					foreach ($del["podaci"] as $del_data){
						$komdel_data = new komitent_delatnost_data();
						
					  	// KOMDEL
							$komdel_data->komdel = $del_data["komdel"];
						
						// ADRESA
							$adrid	= $del_data["adresa_id"];
							$ulica	= $del_data["ulica"];
							$ulid	= $del_data["ulica_id"];
							$broj	= trim($del_data["broj"]);
							$regid	= $del_data["region"];
							$opsid	= $del_data["opstina"];
							$mesid	= $del_data["mesto"];
							$lopsid	= $del_data["lokopstina"];
							$nasid	= $del_data["naselje"];
							
							// provera da li je ukucano ime ulice
							// ili je odabrano
							if(!strlen($ulid)){
								$ul_id = $this->address_mask->getUlicaId($ulica);
								if(!$ul_id){
									if(strlen(trim($ulica))){
										//echo "Unesi novu ulicu: $ulica";
										if($this->address_mask->addUlica($ulica)){		// unos nove ulice
											$ulid = $this->address_mask->getUlicaId($ulica);
										}else{
											die("Greška pri upisu ulice!");
										}
									}else{
										$ulid=-1;
									}
								}else{
									//echo "Sifra ulice: $ulid - $ulica";
									$ulid = $ul_id;
								}
							}
							
							$adr = $this->address_mask->getAddressId($ulid,$broj,$regid,$opsid,$mesid,$lopsid,$nasid);
							if($adr){
								if($adr == $adrid){				// ADRESA NIJE IZMENJENA
									//echo "adresa je ostala ista";
									$komdel_data->adresa = $adrid;
								}else{							// ADRESA JE IZMENJENA - DODELI NOVU
									//echo "Postoji adresa sa datim podacima.";
									$komdel_data->adresa = $adr;
								}
							}else{								// NE POSTOJI ADRESA SA ZADATIM PARAMETRIMA (DODAJ NOVU)
								if($nasid > 0){
									$lid = $nasid;
								}else{
									if($lopsid > 0){
										$lid = $lopsid;
									}else{
										$lid = $mesid;
									}
								}
								// unos nove adrese
								if($this->address_mask->addAdresa($ulid,$lid,$broj)){
									$adr = $this->address_mask->getAddressId($ulid,$broj,$regid,$opsid,$mesid,$lopsid,$nasid);
									$komdel_data->adresa = $adr;
								}else{
									die("Izmene nisu sačuvane. Unknown error.");
								}
							}
						
						// TELEFON I FAKS
							$komdel_data->tel = $del_data["telefon"];
							$komdel_data->fax = $del_data["faks"];
						// RADNO VREME
							$komdel_data->rv = $del_data["rvreme"];
						// PLATNE KARTICE
							$komdel_data->card = $del_data["cards"];
						
						$komdel->data[] = $komdel_data;
					}
				}
				
				$this->delatnosti[] = $komdel;
			}
		}
	}
	/**
	 * Parse URI data and create komitent object
	 *
	 * @param string URIdata
	 * @return object komitent
	 */
	function parseURIData($data){
		$data = utf8RawUrlDecode($data);
		$data = utf8RawChrDecode($data);
		if(strstr($data,"{=}")){
			$level1 = preg_split('/{!}/',$data);			// LEVEL 1
			foreach ($level1 as $key1 => $val1){
				$tmp1 =  preg_split('/{=}/',$val1,2);
				$k1 = $tmp1[0];	$v1 = $tmp1[1];
				
				if(strstr($v1,"{==}")){
					$level2 = preg_split('/{!!}/',$v1);				// LEVEL 2
					foreach ($level2 as $key2 => $val2){
						$tmp2 = preg_split('/{==}/',$val2,2);
						$k2 = $tmp2[0];	$v2 = $tmp2[1];
						
						if(strstr($v2,"{===}")){
							$level3 = preg_split('/{!!!}/',$v2);			// LEVEL 3
							foreach ($level3 as $key3 => $val3){
								$tmp3 = preg_split('/{===}/',$val3,2);
								$k3 = $tmp3[0];	$v3 = $tmp3[1];
								
								if(strstr($v3,"{====}")){
									$level4 = preg_split('/{!!!!}/',$v3);			// LEVEL 4
									foreach ($level4 as $key4 => $val4){
										$tmp4 = preg_split('/{====}/',$val4,2);
										$k4 = $tmp4[0];	$v4 = $tmp4[1];
										
										if(strstr($v4,"{=====}")){
											$level5 = preg_split('/{!!!!!}/',$v4);				// LEVEL 5
											foreach ($level5 as $key5 => $val5){
												$tmp5 = preg_split('/{=====}/',$val5,2);
												$k5 = $tmp5[0];	$v5 = $tmp5[1];
												
												if(strstr($v5,"{======}")){
													$level6 = preg_split('/{!!!!!!}/',$v5);
													foreach ($level6 as $key6 => $val6){
														$tmp6 = preg_split('/{======}/',$val6,2);
														$k6 = $tmp6[0];	$v6 = $tmp6[1];
														
														$object[$k1][$k2][$k3][$k4][$k5][$k6] = $v6;
													}
												}else{
													$object[$k1][$k2][$k3][$k4][$k5] = $v5;
												}
											}
										}else{
											$object[$k1][$k2][$k3][$k4] = $v4;
										}
									}
								}else{
									$object[$k1][$k2][$k3] = $v3;
								}
							}
						}else{
							$object[$k1][$k2] = $v2;
						}
					}
				}else{
					$object[$k1] = $v1;
				}
			}
		}else{
			$object = $data;
		}
		return $object;
	}
	/**
	 * Save komitent in DB finally
	 *
	 */
	function _save(){
		// komitenti
		// komitenti_delatnosti
		// komitenti_telefoni
		// komitenti_emails
		// komitenti_radnici
		// komitenti_ziro_racuni
		// komitenti_webs
		// profili
	}
	/**
	 * Fetch item from passed object
	 *
	 * @param int value
	 * @param array collection
	 * @param string serach key
	 * @param string return key
	 * @return array
	 */
	function getItem($value,$array,$key,$ret_key){
		$ret = null;
		if(!is_array($array)){
			return null;
		}
		foreach ($array as $k=>$v){
			if($v[$key]==$value){
				if(is_array($ret_key)){
					$tmp = array();
					foreach ($ret_key as $rk){
						$tmp[$rk]=$v[$rk];
					}
					$ret[] = $tmp;
				}else{
					$ret[] = $v[$ret_key];
				}
			}
		}
		return $ret;
	}

	//------------------------------------------------------------------------------------------
	//									GET FUNCTIONS
	//------------------------------------------------------------------------------------------

	/**
	 * dovlaci sve tipove komitenata
	 *
	 * @return Array(ids,names);
	 */
	function get_TipoviKomitenata(){
		$res = $this->db->select2("SELECT * FROM komitenti_tipovi ORDER BY kom_tip_name");
		$ret = array();
		foreach ($res as $k=>$v){
			$ret["id"][$k] = $v["kom_tip_id"];
			$ret["name"][$k] = $v["kom_tip_name"];
		}
		return $ret;
	}
	function get_StruktureKomitenata(){
		$res = $this->db->select2("SELECT * FROM komitenti_strukture ORDER BY kom_strukt_name");
		$ret = array();
		foreach ($res as $k=>$v){
			$ret["id"][$k] = $v["kom_strukt_id"];
			$ret["name"][$k] = $v["kom_strukt_name"];
		}
		return $ret;
	}
	function get_ViewStatus($id=null){
		$res = $this->db->select2("SELECT * from komitenti_view_status ORDER BY id");
		$ret = array();
		if($res !== false){
			foreach ($res as $k=>$v){
				$ret["id"][$k] = $v["id"];
				$ret["name"][$k] = $v["name"];
				if($id == $v["id"]){
					$ret["selected"][$k] = " selected";
				}else{
					$ret["selected"][$k] = "";
				}
			}
		}
		return $ret;
		
		/*$vs_ids = array(1,2,3,4,5);
		$vs_names = array("Internet + Knjiga", "Samo knjiga", "Samo internet", "Nije na ovoj adresi","Firma je ugašena");
		$vs_selected = array_fill(0,5,"");
		foreach ($vs_ids as $k=>$v){
			if ($v==$id) {
				$vs_selected[$k] = " selected";
				break;
			}
		}
		return array(
			"id" => $vs_ids,
			"name" => $vs_names,
			"selected" => $vs_selected
		);*/
	}
	function get_NaciniPlacanja(){
		$res = $this->db->select2("SELECT * FROM komitent_nacini_placanja ORDER BY komitent_np_id");
		$ret = array();
		foreach ($res as $k=>$v){
			$ret["id"][$k] = $v["komitent_np_id"];
			$ret["name"][$k] = $v["komitent_np_name"];
		}
		return $ret;
	}
	function get_Banke(){
		$res = $this->db->select2("SELECT banka_id, banka_name, banka_code FROM banke ORDER BY banka_name");
		$ret = array();
		foreach ($res as $k=>$v){
			$ret["id"][$k] = $v["banka_id"];
			$ret["name"][$k] = $v["banka_name"];
			$ret["code"][$k] = $v["banka_code"];
		}
		return $ret;
	}
	function get_Radnici(){
		$res = $this->db->select2("SELECT radnik_id, radnik_ime, radnik_prezime, CONCAT(radnik_ime,' ',radnik_prezime) as sort FROM radnici ORDER BY sort");
		$ret = array();
		foreach ($res as $k=>$v){
			$ret["id"][$k] = $v["radnik_id"];
			$ret["name"][$k] = $v["sort"];
			$ret["prez"][$k] = $v["radnik_prezime"];
			$ret["ime"][$k] = $v["radnik_ime"];
		}
		return $ret;
	}
	function get_Kampanje($kamid=null){
		$res = $this->db->select2("SELECT * FROM kampanje ORDER BY kampanja_start_date DESC");
		$ret = array();
		foreach ($res as $k=>$v){
			$ret["id"][$k] = $v["kampanja_id"];
			$ret["name"][$k] = $v["kampanja_naziv"];
			$ret["date"][$k] = date($this->getFromConfig("short_date"),$v["kampanja_start_date"]);
			if($v["kampanja_id"] ==$kamid){
				$ret["selected"][$k]=" selected";
			}else{
				$ret["selected"][$k]="";
			}
			if($v["kampanja_end_date"])
				$ret["date"][$k] .= " - ".date($this->getFromConfig("short_date"),$v["kampanja_end_date"]);
		}
		return $ret;
	}
	function get_Kategorije($katid=null){
		$res = $this->db->select2("SELECT kat_id, kat_ime FROM kategorije ORDER BY kat_ime");
		$ret = array();
		foreach ($res as $k=>$v){
			$ret["id"][$k] = $v["kat_id"];
			$ret["name"][$k] = $v["kat_ime"];
			if($v["kat_id"] == $katid){
				$ret["selected"][$k] = " selected";
			}else{
				$ret["selected"][$k] = "";
			}
		}
		return $ret;
	}
	function get_Delatnosti($delid=null){
		$res = $this->db->select2("SELECT delatnost_id, delatnost_ime, delkat_katid FROM delatnosti, delatnosti_kategorije WHERE delatnosti_kategorije.delkat_delid=delatnosti.delatnost_id ORDER BY delatnost_ime");
		$ret = array();
		foreach ($res as $k=>$v){
			$ret["id"][$k] = $v["delatnost_id"];
			$ret["name"][$k] = $v["delatnost_ime"];
			$ret["kategorija"][$k] = $v["delkat_katid"];
			if($v["delatnost_id"] == $delid){
				$ret["selected"][$k] = " selected";
			}else{
				$ret["selected"][$k] = "";
			}
			$parent = (int) $this->db->getOne("SELECT 1 FROM delatnosti_nasledjivanje WHERE parent=".$v["delatnost_id"]." LIMIT 1");
			$ret["parent"][$k] = $parent>0?"parent":"child";
		}
		return $ret;
	}
	function get_ProfiliTipovi($id=null){
		$res = $this->db->select2("SELECT * FROM profili_tipovi ORDER BY code");
		$ret = array();
		foreach ($res as $k=>$v){
			$ret["id"][$k] = $v["id"];
			$ret["name"][$k] = $v["code"];
			$ret["desc"][$k] = $v["desc"];
			if($id == $v["id"]){
				$ret["selected"][$k] = " selected";
			}else{
				$ret["selected"][$k] = "";
			}
		}
		return $ret;
	}
	function get_BaneriTipovi($id=null){
		$res = $this->db->select2("SELECT * FROM baneri_tipovi ORDER BY name");
		$ret = array();
		if($res[0]){
			foreach ($res as $k=>$v){
				$ret["id"][$k] = $v["id"];
				$ret["name"][$k] = $v["name"];
				$ret["desc"][$k] = $v["desc"];
				if($id == $v["id"]){
					$ret["selected"][$k] = " selected";
				}else{
					$ret["selected"][$k] = "";
				}
			}
		}
		return $ret;
	}
}
class komitent_delatnost{
	var $id;
	var $name;
	var $glavna;
	var $profil_srp;
	var $profil_eng;
	
	var $email	= array();
	var $web	= array();
	var $brand	= array();
	var $kword_srb	= array();
	var $kword_eng	= array();
	var $data = array();
	function komitent_delatnost($id){
		$this->id = $id;
	}
}
class komitent_delatnost_data{
	var $komdel;
	var $adresa;
	var $tel	= array();
	var $fax	= array();
	var $rv 	= array("rd"=>"","sub"=>"","ned"=>"");
	var $card	= array();
	
	var $www_params;
	/*var $dat_pos;
	var $dat_istek;
	var $dat_hosting;
	var $logo;
	var $tip_profila;
	var $baner_tip;
	var $baner_name;
	var $baner_start;
	var $baner_end;
	var $test_adresa;
	var $kon_adresa;*/
}
?>